import {
  getGarageList,
  editGarageList
} from '@/api/garage'

const state = {
  garageData: [],
  garageDataCount: 0
}

const mutations = {
  GET_GARAGE_DATA: (state, data) => {
    state.garageData = data
  },
  GET_GARAGE_DATA_COUNT: (state, data) => {
    state.garageDataCount = data
  }
}

const actions = {
  async getGarageData({ commit }, playload) {
    let data = {}
    let newData = []
    if (Object.keys(playload).length > 0) {
      data = playload
    }
    const resp = await getGarageList(data)
    const host = resp.data.host
    resp.data.data.rows.map((item) => {
      newData = [...newData, {
        id: item.id,
        host: host,
        cartype0: item.cartype0,
        cartype1: item.cartype1,
        dayprice: item.dayprice,
        dayprice0: item.dayprice0,
        maxspeed: item.maxspeed,
        speed100time: item.speed100time,
        state: item.state,
        des: item.des,
        ctime: item.ctime,
        icon: item.icon,
        img: item.img,
        content: item.content,
        sort: item.sort,
        state0: item.state0,
        type0: item.type0,
        type1: item.type1,
        parseURL: item.parseURL
      }]
    })
    // for (let i = 0; i < 8; i++) {
    //   newData = [...newData, {
    //     id: i + 1,
    //     host: 'http://static.baisha.work',
    //     cname: '阿斯顿马丁',
    //     ctype: '超豪华跑车',
    //     drent: 5000,
    //     sdrent: 4000,
    //     adcontent: '挑战极限，',
    //     ctime: '2019-04-24 14:58:00',
    //     state: 0,
    //     sort: 1,
    //     img: '/upload/car/20190506/wXMG.jpg',
    //     carimg: '/upload/car/20190506/s653.jpg',
    //     content: '<h3>您好</h3>'
    //   }]
    // }
    commit('GET_GARAGE_DATA', newData)
    commit('GET_GARAGE_DATA_COUNT', resp.data.data.total)
    return resp
  },
  async editGarageData({ commit }, playload) {
    const resp = await editGarageList(playload)
    return resp
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
